package com.atkid.boot.controller;


import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.atkid.boot.entity.Student;
import com.atkid.boot.entity.vo.StuVo;
import com.atkid.boot.entity.vo.StudentVo;
import com.atkid.boot.result.Result;
import com.atkid.boot.service.StudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kid
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/boot/student")
public class StudentController {
	@Autowired
	StudentService studentService;

	@GetMapping("/{current}/{limit}")
	public Result getAllStudent(@PathVariable Long current, @PathVariable Long limit) {
		Page<Student> page = studentService.getAllStudent(current, limit);
		return Result.ok().data("page",page);
	}

	@GetMapping("/vo/{current}/{limit}")
	public Result getAllStudentVo(@PathVariable Long current,@PathVariable Long limit) {
		Page<Student> page = studentService.getAllStudent(current, limit);
		Page<StudentVo> voPage = studentService.transferStu(page);
		return Result.ok().data("voPage",voPage);
	}

	@GetMapping("/{id}")
	public Result getStudentById(@PathVariable String id) {
		Student student = studentService.getById(id);
		return Result.ok().data("student",student);
	}

	/**
	 * <h2>student登录方法</h2>
	 * @return is login?
	 */
	@PostMapping("/login")
	public Result login(@RequestBody Student student) {
		String id = student.getId();
		if (id.length() != 12 && id.length() != 10) {
			return Result.error();
		}
		Student s = studentService.getById(student.getId());
		// 如果用户之前没有登录
		if (s == null) {
			this.saveStudent(student);
			StudentVo vo = studentService.getVoByStudent(student);
			return Result.ok().message("用户登录成功：首次登录").data("vo",vo);
		}

		if (!s.getName().equals(student.getName())) {
			return Result.error().message("学号或姓名错误");
		}

		// 如果非首次登录，需要更新学生信息
		s.setDepartment(student.getDepartment());
		s.setClassName(student.getClassName());
		studentService.updateById(s);

		StudentVo vo = studentService.getVoByStudent(s);

		return Result.ok().message("用户登录成功：非首次登录").data("vo",vo);
	}

	/**
	 * <h2>student登录方法</h2>
	 * @return is login?
	 */
	@PostMapping("/card")
	public Result Card(@RequestBody Student student) {
		String id = student.getId();
		if (id.length() != 12 && id.length() != 10) {
			return Result.error();
		}
		Student s = studentService.getById(student.getId());
		if (!s.getName().equals(student.getName())) {
			return Result.error().message("学号或姓名错误");
		}
		StudentVo vo = studentService.getVoByStudent(s);
		return Result.ok().message("用户登录成功").data("vo",vo);
	}


	@PostMapping
	public Result saveStudent(@RequestBody Student student) {


		Integer club1Id = student.getClub1Id();
		Integer club2Id = student.getClub2Id();

		Date date = new Date();

		if (club1Id != null) {
			student.setTime1(date);
		}

		if (club2Id != null) {
			student.setTime2(date);
		}

		studentService.save(student);
		return Result.ok().message("成功保存学生数据");
	}

	@PutMapping
	public Result updateStudent(@RequestBody Student student) {
		Student s = studentService.getById(student.getId());
		if (s == null) {
			return Result.error().message("未查找到相应学生");
		}

		Integer club1Id = student.getClub1Id();
		Integer club2Id = student.getClub2Id();

		Integer oldClub1Id = s.getClub1Id();
		Integer oldClub2Id = s.getClub2Id();
		s.setClub1Id(club1Id);
		s.setClub2Id(club2Id);

		Date date = new Date();

		// 如果选择过社团信息
		if (club1Id != null) {
			// 如果新社团与旧社团不同，则需要更新时间
			if (!club1Id.equals(oldClub1Id)) {
				s.setTime1(date);
			}
		} else {
			s.setTime1(null);
		}

		if (club2Id != null) {
			if (!club2Id.equals(oldClub2Id)) {
				s.setTime2(date);
			}
		} else {
			s.setTime2(null);
		}
		studentService.updateById(s);
		return Result.ok().message("更新学生信息成功");
	}

	@PutMapping("/club/{studentId}")
	public Result updateClub(@PathVariable String studentId,@RequestParam(required = false) Integer club1Id,@RequestParam(required = false) Integer club2Id) {
		Student s = studentService.getById(studentId);
		if (s == null) {
			return Result.error().message("未查找到相应学生");
		}


		Integer oldClub1Id = s.getClub1Id();
		Integer oldClub2Id = s.getClub2Id();

		s.setClub1Id(club1Id);
		s.setClub2Id(club2Id);

		Date date = new Date();

		// 如果选择过社团信息
		if (club1Id != null) {
			// 如果新社团与旧社团不同，则需要更新时间
			if (!club1Id.equals(oldClub1Id)) {
				s.setTime1(date);
			}
		} else {
			s.setTime1(null);
		}

		if (club2Id != null) {
			if (!club2Id.equals(oldClub2Id)) {
				s.setTime2(date);
			}
		} else {
			s.setTime2(null);
		}
		studentService.updateById(s);
		return Result.ok().message("成功修改社团信息");
	}

	@CurrentLimiter(QPS = 30)
	@GetMapping("/club/{cid}")
	public Result getVoByClub(@PathVariable int cid){
		List<StuVo> res = studentService.getByClub(cid);
		return Result.ok().data("student",res);
	}

	@CurrentLimiter(QPS = 30)
	@GetMapping("/club/{cid}/{current}/{size}")
	public Result getVoByClub(@PathVariable int cid,@PathVariable Long current,@PathVariable Long size){
		Page<StuVo> res = studentService.getByClub(cid,current,size);
		return Result.ok().data("student",res);
	}

	@GetMapping("/count/{cid}")
	public Result getCountByClubId(@PathVariable Integer cid) {
		int count = studentService.getCountByClubId(cid);
		return Result.ok().data("count",count);
	}
}

